# APKBUILD based on linux-vanilla aport. Changes:
# - disabled module installation
# - add !check !tracedeps
# - package: just install zimage and kernel.release, because the kernel config
#	does not generate modules or dtb files
# - do not create -dev subpackage (makes no sense without module support)
#
# Kernel config based on: arch/arm/configs/lineageos_mako_defconfig
# Changes:
# - enable devtmpfs (needed for udev -> touch support in weston)

_vendor=archos
_flavor=archos-a80
_hash="cee8b37673ca8de18b563ab76bc94e9b111083e1"
_config="config-${_flavor}.armhf"

pkgname=linux-${_flavor}
pkgver=3.0.8
case $pkgver in
	*.*.*)  _kernver=${pkgver%.*};;
	*.*) _kernver=$pkgver;;
esac
pkgrel=0
arch="armhf"
pkgdesc="Linux for Archos 80 G9"
url="https://gitorious.org/archos/archos-gpl-gen9-kernel-ics.git"
depends="postmarketos-mkinitfs"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev"
options="!strip !check !tracedeps"
install=
source="
	${pkgname}-${_hash}.tar.gz::https://gitorious.org/archos/archos-gpl-gen9-kernel-ics/blobs/master/linux.config?p=archos:archos-gpl-gen9-kernel-ics.git;a=snapshot;h=${_hash};sf=tgz
	$_config
	compiler-gcc6.h
"
subpackages=""
license="GPL2"

_abi_release=${pkgver}
_carch="arm"
HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

ksrcdir="$srcdir/archosarchos-gpl-gen9-kernel-ics-${_hash:0:7}"

prepare() {
	local _patch_failed=
	cd "$ksrcdir"

	# first apply patches in specified order
	for i in $source; do
		case $i in
		*.patch)
			msg "Applying $i..."
			if ! patch -s -p1 -N -i "$srcdir"/$i; then
				echo $i >>failed
				_patch_failed=1
			fi
			;;
		esac
	done

	if ! [ -z "$_patch_failed" ]; then
		error "The following patches failed:"
		cat failed
		return 1
	fi

	# gcc6 support
	cp -v "$srcdir/compiler-gcc6.h" "$ksrcdir/include/linux/"

	mkdir -p "$srcdir"/build
	cp "$srcdir"/$_config "$srcdir"/build/.config || return 1
	make -C "$ksrcdir" O="$srcdir"/build ARCH="$_carch" HOSTCC="$HOSTCC" \
		oldconfig
}



# this is so we can do: 'abuild menuconfig' to reconfigure kernel
menuconfig() {
	cd "$srcdir"/build || return 1
	make ARCH="$_carch" menuconfig
	cp .config "$startdir"/$_config
}

build() {
	cd "$srcdir"/build
	unset LDFLAGS
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-Alpine" \
		|| return 1
}

package() {
	install -Dm644 "$srcdir/build/arch/arm/boot/zImage" \
		"$pkgdir/boot/vmlinuz-$_flavor"

	install -D "$srcdir/build/include/config/kernel.release" \
		"$pkgdir/usr/share/kernel/$_flavor/kernel.release"
}

sha512sums="79c11e1e22d2d8a32e247634707861485b5750cad79edcc0fe202775dcd7127cff48b83fba9249c3ce2a652125f59494b768bb68c1098b99bdfbab13e3659979  linux-archos-a80-cee8b37673ca8de18b563ab76bc94e9b111083e1.tar.gz
096da63fe49cf90708ade7742d1e3526f2a50c24ec2f9a0ed7e44ff43f6e9dd6d1af06c5f48a10736d3a18b8d80379bd3cd6db49385608d26f60bfb5ff29efca  config-archos-a80.armhf
d80980e9474c82ba0ef1a6903b434d8bd1b092c40367ba543e72d2c119301c8b2d05265740e4104ca1ac5d15f6c4aa49e8776cb44264a9a28dc551e0d1850dcc  compiler-gcc6.h"
